Add custom ExchangeId generator support#788
Merged
ok2c merged 1 commit intoapache:masterfrom Jan 19, 2026
Merged
Conversation
rschmitt
requested changes
Jan 16, 2026
httpclient5/src/main/java/org/apache/hc/client5/http/protocol/HttpClientContext.java
Outdated
Show resolved
Hide resolved
d6326b4 to
5069736
Compare
ok2c
reviewed
Jan 17, 2026
httpclient5/src/main/java/org/apache/hc/client5/http/ExchangeIdGenerator.java
Outdated
Show resolved
Hide resolved
Contributor
|
Just for curiosity, how do you plan to use this? |
Contributor
Author
@rschmitt Thank you for the feedback. I understand your curiosity. // My custom ExchangeId
final String exchangeId = "myexec-001";
final CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create().build();
final SimpleHttpRequest request = new SimpleHttpRequest(Method.GET, URI.create("http://somehost"));
final HttpClientContext context = HttpClientContext.create();
// Sets my custom ExchangeId
context.setExchangeId(exchangeId);
client.execute(request, context, null);Of course, although this approach is simple to use, it is not appropriate. With the current changes, I will pass the custom final class MyRequestProducer implements AsyncRequestProducer {
private final String myExchangeId;
private final HttpRequest httpRequest;
private final AsyncEntityProducer entityProducer;
@Override
public void sendRequest(final RequestChannel channel, final HttpContext context) throws HttpException, IOException {
// Sets my custom ExchangeId
MyExchangeIdGenerator.set(this.myExchangeId);
channel.sendRequest(this.httpRequest, this.entityProducer, context);
}
}
final class MyExchangeIdGenerator implements Supplier<String> {
static final ThreadLocal<String> THREADLOCAL = new ThreadLocal<>();
static void set(final String exchangeId) {
THREADLOCAL.set(exchangeId);
}
@Override
public String get() {
final String myExchangeId = THREADLOCAL.get();
if (myExchangeId == null) {
return ExecSupport.getNextExchangeId();
}
THREADLOCAL.remove();
return myExchangeId;
}
}final String exchangeId = "myexec-001";
final Supplier<String> exchangeIdGenerator = new MyExchangeIdGenerator();
final CloseableHttpAsyncClient client = HttpAsyncClientBuilder.create().setExchangeIdGenerator(exchangeIdGenerator).build();
final SimpleHttpRequest request = new SimpleHttpRequest(Method.GET, URI.create("http://somehost"));
final MyRequestProducer producer = new MyRequestProducer(exchangeId, request, null);
client.execute(producer, null, null); |
Member
@yhzdys I was actually going to propose that as a simpler alternative, but your current approach is more generic and is fine with me as well @rschmitt Do you have any objections to merging the PR? |
rschmitt
approved these changes
Jan 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add ExchangeIdGenerator interface to allow users to customize exchange ID generation in HTTP clients.
This change introduces the ExchangeIdGenerator interface, which enables users to provide custom strategies for generating exchange identifiers. This is particularly useful in distributed systems where custom tracing and correlation mechanisms are required.